package engine.game.creature.states 
{
	import engine.game.creature.Creature;
	import flash.errors.IllegalOperationError;
	import engine.game.Constants;
	
	/**
	 * This is the base class for all creature states. It contains the methods
	 * that the creature class will wrap, to perform contextual logic.
	 * @author Jon Smelquist
	 */
	public class CreepingState extends ICreatureState
	{
		public function CreepingState(creature:Creature)
		{ 
			super(creature);
		}
		
		/**
		* This function is the main update function that gets called every time the processTick
		* function calculates that it is time. In this function we perform any animation counting
		* or logic that needs to be executed for the respective state.
		**/
		public override function update():void
		{
			
			// Choose a random direction from 0-3
			_creature.animRow = Math.round(Math.random() * (3 - 0)) + 0;
			
			// Amount to move in each direction this update
			var xmov:int = 0;
			var ymov:int = 0;

			if (_creature.animCol == 0) {
				_creature.animCol = 1;
			} else if (_creature.animCol == 1) {
				_creature.animCol = 3;// its 3 because 3%3 would give us state 0 again while distinguishing it for moving to state 2
			} else if (_creature.animCol == 3) {
				_creature.animCol = 2;
			} else if (_creature.animCol == 2) {
				_creature.animCol = 0;
			}

			if (_creature.animRow > 1) {// East and west movement
				xmov = 8;//4 pixels for one step
			} else {// North and South movement
				ymov = 8;//4 pixels for one step
			}
			
			if (_creature.animRow == Constants.EAST && xmov != 0) {
				_creature.position.x += xmov;
			}
			else if (_creature.animRow == Constants.WEST && xmov != 0) {
				_creature.position.x -= xmov;
			}
			else if (_creature.animRow == Constants.SOUTH && ymov != 0) {
				_creature.position.y += ymov;
			}
			else if (_creature.animRow == Constants.NORTH && ymov != 0) {
				_creature.position.y -= ymov;
			}
			
			// Subtract amount of steps walked
			_creature.animCount-=8;
			if (_creature.animCount <= 0) {
				//stand();
			}
		}
		
		/**
		* This function will perform the required logic to stop the creature and return to a base
		* state of just standing. Note: A StandingState for example would execute no logic here
		* since the creature is already standing.
		**/
		public override function stand():void
		{
			_creature.changeState(_creature.standingState);
		}
		
		/**
		* This function performs the logic to decide a direction and initiate a walk in that
		* direction. Note: A CreepingState for example would execute no logic here since
		* the creature is already creeping.
		**/
		public override function creep():void
		{
			// Nothing to do, can not start creeping if we are already creeping!
		}
	}
	
}